package com.front;

import com.admin.entity.SysUser;
import com.admin.service.SysUserService;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.module.office.entity.SysOffice;
import com.admin.sys.base.module.office.service.SysOfficeService;
import com.admin.sys.base.module.role.service.SysRoleService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *【后台用户】页面接口
 */
@RestController
@RequestMapping("/api/sysUser")
public class ApiUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysOfficeService sysOfficeService;

    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<SysUser> page, SysUser sysUser) {
        IPage<SysUser> iPage = sysUserService.getPage(page, sysUser);
        List<SysUser> records = iPage.getRecords();
        for (SysUser record : records) {
            SysOffice sysOffice = sysOfficeService.get(record.getOfficeId());
            record.setOfficeName(sysOffice.getName());
        }
        return ResultInfo.ok("获取分页成功", iPage);
    }

    @RequestMapping("/getList")
    public ResultInfo getList(SysUser sysUser) {
        sysUser.setDelFlag(0);
        List<SysUser> list = sysUserService.getList(sysUser);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
     * 【后台用户】根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    public ResultInfo get(String id) {
        SysUser sysUser = sysUserService.get(id);
        SysOffice sysOffice = sysOfficeService.get(sysUser.getOfficeId());
        sysUser.setOfficeName(sysOffice.getName());
        return ResultInfo.ok("获取对象成功", sysUser);
    }

    /**
     * 【医生信息】获取列表数据
     */
    @RequestMapping("/getOfficeSysUser")
    public ResultInfo getOfficeSysUser(SysUser sysUser) {
        Map<String,Object> map = new HashMap<>();
        sysUser.setRoleCode("doctorRole");
        List<SysUser> list = sysUserService.getList(sysUser);
        List<String> officeList = new ArrayList<>();
        List<JSONObject> sysUserList = new ArrayList<>();
        Map<String, List<SysUser>> collect = list.stream().collect(Collectors.groupingBy(SysUser::getOfficeId));
        for (Map.Entry<String, List<SysUser>> entry : collect.entrySet()) {
            JSONObject json = new JSONObject();
            SysOffice sysOffice = sysOfficeService.get(entry.getKey());
            officeList.add(sysOffice.getName());
            json.put("title", sysOffice.getName());
            for (SysUser entity : entry.getValue()) {
                entity.setOfficeName(sysOffice.getName());
                entity.setOffice(sysOffice);
            }
            json.put("list", entry.getValue());
            sysUserList.add(json);
        }
        map.put("left",officeList);
        map.put("main",sysUserList);
        return ResultInfo.ok("获取列表成功", map);
    }
    
}



